#include "thread.h"
#include"clamp.h"

const float Thread::maxThreadLong = 5.0f;
const float Thread::minThreadLong = 2.5f;

Thread::Thread(const Cube& threadCube, const Cube& clampCube, const Point& direction,const std::map<std::string,ObjectModelPtr>& models)
	: SceneObject(threadCube, direction,models.find(Thread::name())->second){
	Clamp* clamp= new Clamp(clampCube, direction,models);
	clamp->setParent(this);
	this->addObject(Clamp::name(),clamp);
}

Thread::~Thread(){
}

void Thread::timeStep(){
	this->findObject(Clamp::name()).timeStep();
}

void Thread::setModelMatrix(){
	Point tr=this->getDimensions().center();
	this->getParent().setSubModelMatrix(Thread::name());
	glTranslatef(tr.x1, tr.x2,tr.x3);
	glScalef(this->getDimensions().width(), this->getDimensions().depth(), this->getDimensions().height());
}

void Thread::setDirection(const Point& point){
	SceneObject::setDirection(point);
	this->findObject(Clamp::name()).setDirection(point);
}

void Thread::move(float dx,float dy,float dz){
	this->findObject(Clamp::name()).move(dx,dy,dz);
}

void Thread::setSubModelMatrix(const std::string& name){
	this->getParent().setSubModelMatrix(name);
}

void Thread::rotate(float angle){
	this->findObject(Clamp::name()).rotate(angle);
}

void Thread::extend(float offset){
	if(this->dimensions.height()+offset>maxThreadLong)
		return;
	this->dimensions.extend(0,0,-offset);
	this->findObject(Clamp::name()).move(0,0,-offset);
}

void Thread::shrink(float offset){
	if(this->dimensions.height()-offset<minThreadLong)
		return;
	this->move(0,0,-offset);
	this->dimensions.shrink(0,0,-offset);
	this->findObject(Clamp::name()).move(0,0,2*offset);
}
